package com.iaiai.notepad;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.iaiai.notepad.util.DateUtil;

/**
 * 
 * <p>
 * Title: FileActivity.java
 * </p>
 * <p>
 * E-Mail: 176291935@qq.com
 * </p>
 * <p>
 * QQ: 176291935
 * </p>
 * <p>
 * Http: iaiai.iteye.com
 * </p>
 * <p>
 * Create time: 2011-12-4
 * </p>
 * 
 * @author 丸子
 * @version 0.0.1
 */
public class FileActivity extends Activity {

	private Button btnTop;

	private Button btnReturn;

	private ListView listView;

	private FileAdapter adapter;

	private TextView urlAddress;

	private String url = "/"; // 当前地址

	private ProgressDialog loadingDialog; // loading

	private void init() {
		loadingDialog = new ProgressDialog(this);
		loadingDialog.setMessage("Loading...");
		loadingDialog.setCancelable(false);

		btnTop = (Button) findViewById(R.id.btnTop);
		btnReturn = (Button) findViewById(R.id.btnReturn);
		listView = (ListView) findViewById(R.id.listView);
		urlAddress = (TextView) findViewById(R.id.urlAddress);

		btnTop.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (!url.equals("/")) {
					String temp = url.substring(0, url.lastIndexOf("/"));
					temp = temp.substring(0, temp.lastIndexOf("/"));
					final String address = temp.length() <= 0 ? "/" : temp;
					loadingDialog.show();

					final Handler handler = new Handler();
					new Thread() {
						public void run() {
							handler.post(new Runnable() {
								public void run() {
									updateAddress(address);
									loadingDialog.cancel();
								}
							});
						}
					}.start();
				}
			}
		});
		btnReturn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

		urlAddress.setText(url == null ? "/" : url);
		// 配置适配器
		adapter = new FileAdapter(FileActivity.this, getData(url));

		// 添加并且显示
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				final Map<String, Object> map = (Map<String, Object>) adapter
						.getItem(position);
				if (Boolean.parseBoolean(map.get("type").toString())) { // 判断是否是文件夹
					Bundle bundle = new Bundle();
					bundle.putString("folder", url+map.get("filename"));
					
					Intent it = new Intent(FileActivity.this, EditNotepadActivity.class);
					it.putExtras(bundle);
					startActivity(it);
					finish();
				} else { // 不是文件
					loadingDialog.show();

					final Handler handler = new Handler();
					new Thread() {
						public void run() {
							handler.post(new Runnable() {
								public void run() {
									updateAddress(url
											+ map.get("filename").toString());
									loadingDialog.cancel();
								}
							});
						}
					}.start();
				}
			}
		});
	}

	/**
	 * 更新地址
	 * 
	 * @param filename
	 */
	private void updateAddress(String filename) {
		urlAddress.setText(filename);

		adapter.setData(getData(filename));
		url = filename + "/";
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.file);

		init();
	}

	/**
	 * 获得数据
	 * 
	 * @return
	 */
	private List<Map<String, Object>> getData(String address) {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		File root = new File(address);
		File[] files = root.listFiles();
		if (files != null) {
			for (File file : files) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("filename", file.getName());
				map.put("date",
						DateUtil.getDateTime(new Date(file.lastModified())));
				map.put("size", file.length());
				map.put("type", file.isFile()); // 存储是否是文件，true是，false不是

				list.add(map);
			}
		}
		return list;
	}

}
